package com.recharge.web.aop;

import com.google.gson.Gson;
import com.recharge.common.enums.ErrorCode;
import com.recharge.common.result.ResultDO;
import com.recharge.common.util.DESUtil;
import com.recharge.domain.vo.*;
import com.recharge.service.ProviderService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Aspect
@Component
public class DecryptParamAop {

    private Logger logger = LoggerFactory.getLogger(this.getClass().getName());

    @Autowired
    private ProviderService providerService;

    /**
     * 订单请求
     */
    @Pointcut("@annotation(com.recharge.common.annotation.DecryptParam)")
    public void pointCut() {}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        Object[] args = joinPoint.getArgs();

        EncryptionRechargeDO encryptionRecharge = null;

        for (Object obj : args) {
            if (obj instanceof EncryptionRechargeDO) {
                encryptionRecharge = (EncryptionRechargeDO) obj;
                break;
            }
        }
        if (encryptionRecharge == null) {
            throw new Exception(joinPoint.getSignature().getName() + "，参数中无待解密类");
        }
        String jsonData = DESUtil.decrypt(encryptionRecharge.getPrivateKey(), encryptionRecharge.getEncryptionData());
        Gson gson = new Gson();
        RechargeDO rechargeDO = gson.fromJson(jsonData, RechargeDO.class);
        encryptionRecharge.setRechargeDO(rechargeDO);
        return joinPoint.proceed();
    }

    /**
     * 卡密充值卡添加
     */
    @Pointcut("execution(* com.recharge.web.controller.ApiController.appendCard(..))")
    public void cardPointCut(){}

    @Around("cardPointCut()")
    public Object cardAround(ProceedingJoinPoint joinPoint) throws Throwable{
        logger.info("卡密添加切点");
        Object[] args = joinPoint.getArgs();

        EncryptionCardDO encryptionCard = null;

        for (Object obj : args) {
            if (obj instanceof EncryptionCardDO) {
                encryptionCard = (EncryptionCardDO) obj;
                break;
            }
        }

        if (encryptionCard == null) {
            logger.info("Exception");
            throw new Exception(joinPoint.getSignature().getName() + "，参数中无待解密类");
        }
        providerService.get(encryptionCard.getProviderNo());
        ResultDO result = providerService.getByProviderNo(encryptionCard.getProviderNo());
        if (!result.isSuccess()) {
            logger.info("API_PROVIDER_NOT_ERROR");
            throw new Exception(ErrorCode.API_PROVIDER_NOT_ERROR.getMsg());
        }
        ProviderDO providerDO = (ProviderDO) result.getModel(ResultDO.FIRST_MODEL_KEY);
        String jsonData = DESUtil.decrypt(providerDO.getPrivateKey(), encryptionCard.getEncryptionData());

        Gson gson = new Gson();
        CardDO cardDO = gson.fromJson(jsonData, CardDO.class);
        encryptionCard.setCardDO(cardDO);
        return joinPoint.proceed();
    }

}
